package schedulingSystem;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import schedulingSystem.Timetable;

public class Scheduler {

	private ArrayList<Student> _studentList;
	private String _startTime;
	private String _duration;
	private int _group;
	private String _roomNum;
	private int _minGroupSize;
	private int _normalGroupSize;
	private HashMap<String,LinkedList<Student> > listWithClassNameAndStudent;
	
	public Scheduler() {
		_studentList = new ArrayList<Student>(); 
		this._startTime = null;
		this._duration = null;
		this._group = 0;
		this._roomNum = null;
		this._minGroupSize = 0;
		this._normalGroupSize = 0;
	}
	private int countStudent() {
		return _studentList.size();
	}

//	private LinkedList<Student> getPartOfStudentFromClass(String className, int numberOfStudent, char sex) {
//		LinkedList test = new LinkedList();
//		return test;
//	}
//
//	private void assignStudentListToTimeSlot(Timetable timetable, int timeSlot, ArrayList <Student> studentList) {
//		
//	}

	private HashMap<String,LinkedList<Student> > separateStudentFromClass(ArrayList <Student> studentList) {
		//	Receive all students as ArrayList from io
		HashMap<String,LinkedList<Student> > listWithClassNameAndStudent=new HashMap<String,LinkedList<Student> >();
		Iterator <Student> studentItr = studentList.iterator();
		while (studentItr.hasNext()) {
			Student s = studentItr.next();
			String classNameOfStudent = s.getClassName();
			Set<String> classSet = listWithClassNameAndStudent.keySet();
			Iterator<String> iter=classSet.iterator();
			boolean classExisted=false;
			while(iter.hasNext()){
				String className=iter.next();
				if(className.equals(classNameOfStudent))
					classExisted=true;
			}
			if(!classExisted){
				LinkedList<Student> specificClassName = new LinkedList<Student>();
				specificClassName.add(s);
				listWithClassNameAndStudent.put(classNameOfStudent, specificClassName);
			}else{
				LinkedList<Student> specificClassName = listWithClassNameAndStudent.get(classNameOfStudent);
				specificClassName.add(s);
			}
		}
		return listWithClassNameAndStudent;
	}
	
	public void schedule(Timetable timetable, ArrayList <Student> studentList, int _normalGroupSize, int _group) {
		HashMap<String,LinkedList<Student> > listWithClassNameAndStudent=separateStudentFromClass(studentList);
		
		System.out.println(listWithClassNameAndStudent);
		// Randomly select one class name to eliminate
		
		// Debug
//		_normalGroupSize = 4;
//		_group = 7;
//		// Debug
//		
//		
//		
//		//	Find out partialStudentList
//		Iterator <Student> studentItr = studentList.iterator();
//		
//		HashMap<String,String> hMap = new HashMap<String, String>();
//		
//		
//		
//		while (studentItr.hasNext()) {
//			Student s = studentItr.next();
//			
//			
//		}
//		
//		
//		
//		int numberOfTimeslot = countStudent()/(_normalGroupSize*_group);
//		
//		for (int i = 0 ; i < numberOfTimeslot; i++) {
//			//assignStudentListToTimeSlot(timetable, 1, partialStudentList);
//		}
//			
//		
	}
}

// public void schedule(Timetable Timetable, Student Student[]) {
//
//
// Date d1 = null;
// java.text.SimpleDateFormat simple1 = new java.text.SimpleDateFormat();
//
//
// try {
// simple1.applyPattern("HHmmss");
// d1 = simple1.parse("131313");
// } catch (Exception e) {
//
// }
//
// Group g1 = new Group(5, 1, d1, "Chinese", 5);
//
// int time = 5;
// int roomNum = g1.getRoomNum();
//
// java.util.Date date;
// java.text.SimpleDateFormat simple = new java.text.SimpleDateFormat();
// // simple.applyPattern("yyyy-MM-dd");
// try {
// /*
// * date = simple.parse("2011-1-1");
// * System.out.println(date.toString());
// */
// simple.applyPattern("HHmmss");
// date = simple.parse("131313");
// Timetable.setDate(date);
//
// System.out.println(date.toString());
// /*
// * simple.applyPattern("yyyy-MM-dd HHmmss"); date =
// * simple.parse("2011-1-1 131313");
// * System.out.println(date.toString());
// */
//
// System.out.println(new java.text.SimpleDateFormat().format(date));
// System.out.println(new java.text.SimpleDateFormat("HHmmss")
// .format(date));
// } catch (Exception e) {
//
// }
//
// Timetable.setSchedule(5, 5, g1);
// Timetable.getSchedule(time, roomNum);
//
// }

